import pytest
import allure
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import time
@allure.feature("存钱罐APP")
class TestCunQian:
    @pytest.fixture(scope='function',autouse=True)
    def setup(self):
        cap_dict = {
            "deviceName": "127.0.0.1:62001",
            "platformName": "Android",
            "platformVersion": "7.1.2",
            "appPackage": "com.lingyue.zebraloan",
            "appActivity": "com.lingyue.banana.activities.BananaLaunchActivity"
        }
        # 初始化驱动
        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", cap_dict)
        # 设置隐式等待
        self.driver.implicitly_wait(15)
        yield
        time.sleep(2)
        self.driver.quit()
    @allure.story("点击元素：存钱罐")
    def test_1(self):
        # 点击弹窗中的同意按钮
        time.sleep(2)
        with allure.step("点击弹窗中的同意按钮"):
            WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((MobileBy.ID, "com.lingyue.zebraloan:id/tv_confirm"))).click()
        # 等待主页面加载
        time.sleep(10)
        # 点击用户故事
        with allure.step("点击用户故事按钮"):
            WebDriverWait(self.driver, 10, 0.5).until(
            EC.presence_of_all_elements_located((MobileBy.CLASS_NAME, "android.widget.ImageView")))[5].click()

        time.sleep(2)
        # 断言页面是否正确
        with allure.step("断言是否出现用户故事"):
            text = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((MobileBy.ID, "com.lingyue.zebraloan:id/tv_title"))).text
        assert text == "用户故事"
    @allure.story("滑动元素：存钱罐")
    def test_2(self):
        # 479,991
        time.sleep(2)
        with allure.step("通过tap点击同意按钮"):
            TouchAction(self.driver).tap(x=479, y=991).perform()
        """
        点击是瞬发的，一定要加一个等待，不然页面还加载出来，直接就点击了
        tap：点击x是479的，y是991
        perform：执行这一组动作
        """
        time.sleep(10)
        # 点击用户故事
        with allure.step("点击用户故事"):
            WebDriverWait(self.driver, 10, 0.5). \
            until(EC.presence_of_all_elements_located((MobileBy.ID, "com.lingyue.zebraloan:id/iv_card")))[3]. \
            click()
        # 等待页面
        time.sleep(3)
        # 点击第二个 345,898
        with allure.step("通过tap点击用户故事第二个"):
            TouchAction(self.driver).tap(x=345, y=898).perform()
        # 等待页面
        time.sleep(2)

        # 492,1412  601,382
        with allure.step("滑动页面"):
            TouchAction(self.driver).press(x=492, y=1412).move_to(x=601, y=382).release().perform()
        """
        press：从开始的地方移动
        move_to：移动到哪里
        release()：松开手指
        perform()：执行这一组动作
        """
        # 获取用户故事
        time.sleep(1)
        with allure.step("断言页面"):
            text = WebDriverWait(self.driver, 10, 0.5).until(
                EC.presence_of_element_located((MobileBy.ID, "com.lingyue.zebraloan:id/tv_title"))).text
        assert text == "用户故事"
